package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.DietFoodCategory;
import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.module.pojo.SystemDietCookbook;
import com.example.fitnessservice.service.DietService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RestController
@RequestMapping("/diets")
public class DietDataController {
    @Resource
    DietService dietService;

    /**
     * 食物库功能
     * 根据食物分类查询食物
     * @param currentPage
     * @param pageSize
     * @param cate
     */
    @GetMapping("/food/{cate}/{currentPage}/{pageSize}") //cate代表分类
    public Result foodFindALl(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @PathVariable Integer cate){
        //根据前端传来的cate判断用户查询食物的种类
        String food_cate = null;
        if(cate == 1){
            food_cate = "减肥";
        }else if(cate == 2){
            food_cate = "增肌";
        }else{
            food_cate = "痛风";
        }
        //查询
        List<DietFoodCategory> foodAll = dietService.findFoodAll(currentPage, pageSize, food_cate);
        Result result = new Result();
        if(foodAll != null){
            //查询成功
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(new PageInfo(foodAll));
        }else{
            //查询失败
            result.setCode(500);
            result.setMsg("查询失败");
        }
        //返回
        return result;
    }

    /**
     * 根据前端用户的BMI指数查询系统食谱
     * @param bmiUser
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/cookbook/{bmiUser}/{currentPage}/{pageSize}")
    public Result findSystemCookbook(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @PathVariable double bmiUser){
        double flag = 0;
        //根据前端传回来的用户BMI指数判断查询哪类的数据
        if(bmiUser - 18.5 <= 0){
            //用户bmi低于bmi最低正常值，提供滋补食谱
            flag = 18.5;
        }else if(bmiUser - 23.9 <= 0 && bmiUser -18.5 > 0){
            //用户bmi在正常bmi范围内，提供正常减脂食谱
            flag = 23.9;
        }else if (bmiUser - 23.9 >= 0){
            //用户超出bmi正常范围，提供超级减脂食谱
            flag = 26.6;
        }
        //设置查询范围
        BigDecimal bmi = (new BigDecimal(flag)).setScale(2, RoundingMode.HALF_UP); //设置京都的同时要设置取舍方式
        //查询
        List<SystemDietCookbook> systemCookbook = dietService.findSystemCookbook(currentPage, pageSize, bmi);
        Result result = new Result();
        if(systemCookbook != null){
            //查询成功
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(new PageInfo(systemCookbook));
        }else{
            //查询失败
            result.setCode(500);
            result.setMsg("查询失败");
        }
        //返回
        return result;
    }

    /**
     * 根据食物名称进行模糊查询
     * @param foodName
     * @return
     */
    @GetMapping("/food/{foodName}")
    public Result findFoodByName(@PathVariable String foodName){
//        System.out.println("执行了");
        //查询
        List<DietFoodCategory> foodsByName = dietService.findFoodByName(foodName);
        Result result = new Result();
        if(foodsByName != null){
            //查询成功
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(foodsByName);
        }else{
            //查询失败
            result.setCode(500);
            result.setMsg("查询失败");
        }
        //返回
        return result;
    }

    /**
     * 创建带有用户id的空食谱
     * @param userId
     * @return
     */
    @PutMapping("/cookbook/{userId}")
    public Result createUserDietCookbookEmpty(@PathVariable int userId){
        int flag = dietService.createUserDietCookbookEmpty(userId);
        Result result = new Result();
        if(flag == 0){
            //创建失败
            result.setCode(500);
            result.setMsg("创建失败");
        }else{
            //创建成功
            result.setCode(200);
            result.setMsg("创建成功");
        }
        //返回
        return result;
    }

    /**
     * 用户点击添加早餐，系统将该食物添加到用户的早餐食物中
     * @param foodId
     * @return
     */
    @PutMapping("/cookbook/breakfast/{foodId}")
    public Result addBreakfast(@PathVariable int foodId){
        //首先查找刚刚生成的用户食谱中为空的食谱id
        int cookbookId = dietService.findEmptyCookbookId();
        //向关联该id的user_diet_cookbook_food表中添加食物与此关联
        int flag = dietService.addBreakfastFood(cookbookId, foodId);
        Result result = new Result();
        if(flag == 0){
            //添加失败
            result.setCode(500);
            result.setMsg("添加失败");
        }else {
            //添加成功
            result.setCode(200);
            result.setMsg("添加成功");
        }
        //返回
        return result;
    }

    /**
     * 用户点击加入午餐，将该食物添加到用户的午餐食谱中
     * @param foodId
     * @return
     */
    @PutMapping("/cookbook/lunch/{foodId}")
    public Result addLunch(@PathVariable int foodId){
        //首先查找刚刚生成的用户食谱中为空的食谱id
        int cookbookId = dietService.findEmptyCookbookId();
        //向关联该id的user_diet_cookbook_food表中添加食物与此关联
        int flag = dietService.addLunchFood(cookbookId, foodId);
        Result result = new Result();
        if(flag == 0){
            //添加失败
            result.setCode(500);
            result.setMsg("添加失败");
        }else {
            //添加成功
            result.setCode(200);
            result.setMsg("添加成功");
        }
        //返回
        return result;
    }

    /**
     * 用户点击加入晚餐，将该食物添加到用户的午餐食谱中
     * @param foodId
     * @return
     */
    @PutMapping("/cookbook/dinner/{foodId}")
    public Result addDinner(@PathVariable int foodId){
        //首先查找刚刚生成的用户食谱中为空的食谱id
        int cookbookId = dietService.findEmptyCookbookId();
        //向关联该id的user_diet_cookbook_food表中添加食物与此关联
        int flag = dietService.addDinnerFood(cookbookId, foodId);
        Result result = new Result();
        if(flag == 0){
            //添加失败
            result.setCode(500);
            result.setMsg("添加失败");
        }else {
            //添加成功
            result.setCode(200);
            result.setMsg("添加成功");
        }
        //返回
        return result;
    }

    /**
     * 保存用户的食谱
     * @return
     */
    @PutMapping("/cook/store/{userId}")
    public Result storeUserDietCookbook(@PathVariable Integer userId){
        //首先查找刚刚生成的用户食谱中为空的食谱id
        int cookbookId = dietService.findEmptyCookbookId();
        //保存食谱
        int flag = dietService.storeUserDietCookbook(userId, cookbookId);
        //
        Result result = new Result();
        if(flag == 1){
            //保存成功
            result.setCode(200);
            result.setMsg("保存成功");
        }else{
            //保存失败
            result.setCode(500);
            result.setMsg("保存失败");
        }
        //返回
        return result;
    }
}
